package com.home.common.framework.core.entity;

import com.home.base.core.context.BaseContextHandler;
import com.home.common.utils.id.SnowFlake;
import com.home.common.utils.reflex.ReflectionUtils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Description: 实体操作类
 * Created by 王大宸 on 2020-09-28 20:04
 * Created with IntelliJ IDEA.
 */
public class EntityUtils {
    private static final SnowFlake snowFlake = new SnowFlake(3, 4);

    /**
     * 雪花算法 id 生成
     */
    public static String getId() {
        return String.valueOf(snowFlake.nextId());
    }

    /**
     * id, createUser, createUserId, createTime, updateTime, updateUser, updateUserId
     */
    public static <T> void toCreatAndUpdateEntity(T entity) {
        toCreateEntity(entity);
        toUpdateEntity(entity);
    }

    /**
     * id, createUser, createUserId, createTime
     */
    public static <T> void toCreateEntity(T entity) {
        // 用户姓名,用户id,租户id
        String userName = BaseContextHandler.getName();
        String userId = BaseContextHandler.getUserId();

        String[] fieldNames = {"id", "createUser", "createUserId", "createTime"};
        Field createTimeField = ReflectionUtils.getAccessibleField(entity, "createTime");
        // 默认值
        Object[] value = null;
        String id = getId(entity, fieldNames[0]);
        if (createTimeField != null && createTimeField.getType().equals(LocalDateTime.class)) {
            value = new Object[]{id, userName, userId, LocalDateTime.now()};
        }
        if (createTimeField != null && createTimeField.getType().equals(String.class)) {
            value = new Object[]{id, userName, userId, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))};
        }
        if (createTimeField != null && createTimeField.getType().equals(Date.class)) {
            value = new Object[]{id, userName, userId, new Date()};
        }
        // 填充默认属性值
        ReflectionUtils.setDefaultValues(entity, fieldNames, value);
    }


    /**
     * updateTime, updateUser, updateUserId
     */
    public static <T> void toUpdateEntity(T entity) {
        String userName = BaseContextHandler.getName();
        String userId = BaseContextHandler.getUserId();

        String[] fieldNames = {"updateTime", "updateUser", "updateUserId"};
        Field updateTime = ReflectionUtils.getAccessibleField(entity, "updateTime");
        Object[] value = null;
        if (updateTime != null && updateTime.getType().equals(LocalDateTime.class)) {
            value = new Object[]{LocalDateTime.now(), userName, userId};
        }
        if (updateTime != null && updateTime.getType().equals(String.class)) {
            value = new Object[]{LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), userName, userId};
        }
        if (updateTime != null && updateTime.getType().equals(Date.class)) {
            value = new Object[]{new Date(), userName, userId};
        }
        // 填充默认属性值
        ReflectionUtils.setDefaultValues(entity, fieldNames, value);
    }


    /***
     * 功能描述：获取id
     *
     * @author 王大宸
     * @date 2019/4/30 10:29
     * @param entity
     * @param field
     * @return java.lang.String
     */
    private static <T> String getId(T entity, String field) {
        if (!ReflectionUtils.hasField(entity, field)) {
            return null;
        }
        Object value = ReflectionUtils.getFieldValue(entity, field);
        if (value != null && !"".equals(value)) {
            return value.toString();
        } else {
            return String.valueOf(snowFlake.nextId());
        }
    }

    /**
     * Description：置空参数
     *
     * @param
     * @return
     * @author 王大宸
     * @date 2019/4/1 20:40
     */
    public static void setNull(Object... args) {
        for (Object o : args) {
            o = null;
        }
    }

}
